package com.bkhech.satoken.controller;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.session.SaSessionCustomUtil;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.bkhech.satoken.service.Cons;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author guowm
 * @date 2023/8/3
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("user/session")
public class SessionController {

    /**
     * 测试注销
     *
     * @return
     */
    @RequestMapping("api")
    public SaResult api() {
        // 1.user session
        final SaSession userSession = StpUtil.getSession();
        final String username = userSession.getString(Cons.usernameKey);

        // 2.token session
        final SaSession tokenSession = StpUtil.getTokenSession();
        // username2 == null,
        // 注意点：userSession 和 tokenSession 数据是隔离的
        final String username2 = tokenSession.getString(Cons.usernameKey);
        log.info("username2 is null: {}", username2);

        // 3.Custom-Session。 获取指定key的 Custom-Session， 相当于一个 map
        SaSession session = SaSessionCustomUtil.getSessionById("goods-10001");

        // 4.匿名session. 未登录场景下获取 Token-Session
        final SaSession anonTokenSession = StpUtil.getAnonTokenSession();

        return SaResult.ok(username);
    }

}
